﻿using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AppblockConfig.Test
{
    using System;
    using System.Configuration;
    using System.IO;
    using AppBlock.Config;
    using AppBlocks;

    [TestClass]
    public class ConfigManagerTest
    {
        [TestMethod]
        public void GetSectionType_Test()
        {
            var section = ConfigManager.GetSection<CustomSection>();
            Assert.IsNotNull(section);
            Assert.IsInstanceOfType(section, typeof(CustomSection));
        }

        [TestMethod]
        public void GetSectionType_Null_Test()
        {
            ConfigManager.LoadConfigurations();
            var section = ConfigManager.GetSection<ConfigurationSection>();
            Assert.IsNull(section);
        }

        [DeploymentItem(@"Config\C1\App.Config", @"Config\C1")]
        [DeploymentItem(@"Config\AppSettings.config", "Config")]
        [TestMethod]
        public void GetConfig_C1_Test()
        {
            ConfigManager.LoadConfigurations(0, "C1");
            Assert.AreEqual(0, ConfigHelper.GetAppSetting<int>("RootConfig"));
            Assert.AreEqual("C1", ConfigHelper.GetAppSetting("System", ""));
            Assert.IsTrue(ConfigHelper.GetConnString("database").ConnectionString.Contains("C1"));
        }

        [DeploymentItem(@"Config\C2\App.Config", @"Config\C2")]
        [DeploymentItem(@"Config\AppSettings.config", "Config")]
        [TestMethod]
        public void GetConfig_C2_Test()
        {
            ConfigManager.LoadConfigurations(0, "C2");
            Assert.AreEqual(0, ConfigHelper.GetAppSetting<int>("RootConfig"));
            Assert.AreEqual("C2", ConfigHelper.GetAppSetting("System", ""));
            Assert.IsTrue(ConfigHelper.GetConnString("database").ConnectionString.Contains("C2"));
        }

        [DeploymentItem(@"Config\C3\App.Config", @"Config\C3")]
        [DeploymentItem(@"Config\AppSettings.config", "Config")]
        [TestMethod]
        public void GetConfig_C3_Test()
        {
            ConfigManager.LoadConfigurations(0, "C3");
            Assert.AreEqual(0, ConfigHelper.GetAppSetting<int>("RootConfig"));
            Assert.AreEqual("C3", ConfigHelper.GetAppSetting("System", ""));
            Assert.IsTrue(ConfigHelper.GetConnString("database").ConnectionString.Contains("C3"));
        }

        [DeploymentItem(@"parent.config")]
        [DeploymentItem(@"Config\AppSettings.config", "Config")]
        [TestMethod]
        public void GetConfig_parent_Test()
        {
            File.Copy(AppDomain.CurrentDomain.BaseDirectory + "\\parent.config", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\parent.config"));
            ConfigManager.LoadConfigurations(1);
            Assert.AreEqual("parent", ConfigHelper.GetAppSetting("parent",""));
            Assert.IsNotNull(ConfigHelper.GetConnString("database_parent"));
        }

        [DeploymentItem(@"parent.config")]
        [DeploymentItem(@"Config\AppSettings.config", "Config")]
        [TestMethod]
        public void Encrypt_Test()
        {
            ConfigManager.EncryptSectionFiles("custom");
            Assert.AreEqual(true, ConfigManager.GetSection<CustomSection>().Enabled);
            ConfigManager.DecryptSectionFiles("custom");
            Assert.AreEqual(true, ConfigManager.GetSection<CustomSection>().Enabled);
        }

    }
}
